import service from "@/utils/request";
import {ElMessage, ElMessageBox} from "element-plus";
interface Response<D>{
    code: number
    data: D[]
    msg: string
}
interface ResponsePaging<D>{
    code: number
    data: {
        list: D[],
        records: D[]
        total: number
    }
    msg: string
}

/**
 * 表格组件通用
 */
class GlobalPage<T, D>{
    // 数据列表 Url
    protected readonly _dataListUrl: string
    protected readonly _fullUrl: string
    protected readonly _method: "GET" | "POST"
    constructor(dataListUrl: string, fullUrl: string, method: "GET" | "POST") {
        this._dataListUrl = dataListUrl;
        this._fullUrl = fullUrl
        this._method = method
    }
    async added(data: any){
        return await service.post(this._dataListUrl, data)
    }
    async update(data: any){
        return await service.put(this._dataListUrl, data)
    }
    async del(id?: number | string){
        return await service.delete(this._dataListUrl + `/${id}`)
    }
    async batchDel(data: any[]){
        return await service.delete(this._dataListUrl, {data})
    }
}
//分页
export class Paging<T, D> extends GlobalPage<T, D>{
    constructor(dataListUrl: string, fullUrl: string, method: "GET" | "POST") {
        super(dataListUrl, fullUrl, method)
    }
    async query(paging: any, params?: T): Promise<ResponsePaging<D>>{
        return await service.get(this._fullUrl? this._fullUrl : this._dataListUrl + "/page", {
            params: {
                ...paging,
                ...params
            }
        })
    }
}
// 不分页
export class Base<T, D> extends GlobalPage<T, D>{
    constructor(dataListUrl: string, fullUrl: string, method: "GET" | "POST") {
        super(dataListUrl, fullUrl, method)
    }
    async query(params?: T): Promise<Response<D>>{
        return await service.get(this._fullUrl? this._fullUrl : this._dataListUrl + "/list", {
            params: {
                ...params
            }
        })
    }
}



